﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Net;
using System.Diagnostics;
using System.Threading;

namespace Minecraft_Xenon
{
    public partial class Form1 : Form
    {
        #region 实例化部件

        //呵呵体检
        private HeheExamination HeheExamination = new HeheExamination();

        #endregion

        #region 窗体加载、绘制、初始化

        #region 窗体边框阴影效果变量申明

        const int CS_DropSHADOW = 0x20000;
        const int GCL_STYLE = (-26);

        //声明Win32 API
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);

        #endregion

        public Form1()
        {
            InitializeComponent();

            //NOTICE:取消非法线程修改窗体的限制，但降低安全性
            Control.CheckForIllegalCrossThreadCalls = false;

            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW); //API函数加载，实现窗体边框阴影效果
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 163 && this.ClientRectangle.Contains(this.PointToClient(new Point(m.LParam.ToInt32()))) && m.WParam.ToInt32() == 2)
                m.WParam = (IntPtr)1;
            base.WndProc(ref m);
            if (m.Msg == 132 && m.Result.ToInt32() == 1)
                m.Result = (IntPtr)2;
        }

        public static int sleft; //声明全局变量：sLeft
        public static int stop;  //声明全局变量：sTop
        public static int fHeight = 420; //窗口高度
        public static int fWidth = 760;  //窗口宽度
        private void Form1_Load(object sender, EventArgs e)
        {

            /*----绘制窗口----*/
            sleft = MainPanel.Left;
            stop = MainPanel.Top;
            this.Width = fWidth;
            this.Height = fHeight;
            this.CenterToScreen(); //居中窗口
            /*----绘制窗口----*/
            pictureBox3.Image = Properties.Resources.index;
            btnBuild.BackgroundImage = Properties.Resources.alph2;
            btnHehe.BackgroundImage = Properties.Resources.alph2;
            btnLaunch.BackgroundImage = Properties.Resources.alph2;
            btnMod.BackgroundImage = Properties.Resources.alph2;
            btnSetting.BackgroundImage = Properties.Resources.alph2;
        }
        #endregion

        #region 修改面板
        /*
        void ChangePanel(int id)
        {
            switch (id)
            {
                case 0:
                    MainPanel.Top = stop;
                    MainPanel.Left = sleft;
                    MainPanel.Visible = true;
                    BuildPanel.Visible = false;
                    hehePanel.Visible = false;
                    pictureBox3.Image = Properties.Resources.index;
                    pictureBox3.Cursor = Cursors.Arrow;
                break;
                case 1:
                    BuildPanel.Top = stop;
                    BuildPanel.Left = sleft;
                    MainPanel.Visible = false;
                    BuildPanel.Visible = true;
                    pictureBox3.Image = Properties.Resources.back;
                    pictureBox3.Cursor = Cursors.Hand;
                break;
                case 2:
                    hehePanel.Top = stop;
                    hehePanel.Left = sleft;
                    MainPanel.Visible = false;
                    hehePanel.Visible = true;
                    pictureBox3.Image = Properties.Resources.back;
                    pictureBox3.Cursor = Cursors.Hand;
                break;
                case 3:

                break;
                case 4:

                break;
                case 5:

                break; 
            }

        }
        */

        private void ChangePanel(Panel target)
        {
            Panel panel = (Panel)this.GetChildAtPoint(new Point(1, 95));

            if (panel == MainPanel)
            {
                pictureBox3.Image = Properties.Resources.back;
                pictureBox3.Cursor = Cursors.Hand;
            }
            else
            {
                pictureBox3.Image = Properties.Resources.index;
                pictureBox3.Cursor = Cursors.Arrow;
            }


            PanelSwitchFX psfx = new PanelSwitchFX(this, panel, target, PanelSwitchFX.SWITCH_TO_LEFT, 300);
            psfx.Show();
        }

        #endregion

        #region 主界面按钮动画效果

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox1.BackgroundImage = Properties.Resources.alpha;
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.BackgroundImage = null;
        }

        private void btnBuild_MouseMove(object sender, MouseEventArgs e)
        {
            btnBuild.BackgroundImage = Properties.Resources.alpha;
        }

        private void btnMod_MouseMove(object sender, MouseEventArgs e)
        {
            btnMod.BackgroundImage = Properties.Resources.alpha;
        }
        private void btnSetting_MouseMove(object sender, MouseEventArgs e)
        {
            btnSetting.BackgroundImage = Properties.Resources.alpha;
        }

        private void btnLaunch_MouseMove(object sender, MouseEventArgs e)
        {
            btnLaunch.BackgroundImage = Properties.Resources.alpha;
        }

        private void btnHehe_MouseMove(object sender, MouseEventArgs e)
        {
            btnHehe.BackgroundImage = Properties.Resources.alpha;
        }

        private void btnBuild_MouseLeave(object sender, EventArgs e)
        {
            btnBuild.BackgroundImage = Properties.Resources.alph2;
        }

        private void btnMod_MouseLeave(object sender, EventArgs e)
        {
            btnMod.BackgroundImage = Properties.Resources.alph2;
        }

        private void btnSetting_MouseLeave(object sender, EventArgs e)
        {
            btnSetting.BackgroundImage = Properties.Resources.alph2;
        }

        private void btnLaunch_MouseLeave(object sender, EventArgs e)
        {
            btnLaunch.BackgroundImage = Properties.Resources.alph2;
        }

        private void btnHehe_MouseLeave(object sender, EventArgs e)
        {
            btnHehe.BackgroundImage = Properties.Resources.alph2;
        }

        private void pictureBox3_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox3.BackgroundImage = Properties.Resources.alpha;
        }

        private void pictureBox3_MouseLeave(object sender, EventArgs e)
        {
            pictureBox3.BackgroundImage = null;
        }

        private void pictureBox5_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox5.BackgroundImage = Properties.Resources.alpha;
        }

        private void pictureBox5_MouseLeave(object sender, EventArgs e)
        {
            pictureBox5.BackgroundImage = null;
        }

        private void pictureBox4_MouseMove(object sender, MouseEventArgs e)
        {
            pictureBox4.BackgroundImage = Properties.Resources.alpha;
        }

        private void pictureBox4_MouseLeave(object sender, EventArgs e)
        {
            pictureBox4.BackgroundImage = null;
        }


        private void btnSwitchPage_MouseMove(object sender, MouseEventArgs e)
        {
            btnSwitchPage.BackgroundImage = Properties.Resources.alpha;
        }

        private void btnSwitchPage_MouseLeave(object sender, EventArgs e)
        {
            btnSwitchPage.BackgroundImage = null;
        }

        
        #endregion

        #region JAVA环境搭建部分
        public string azurl = System.Environment.GetEnvironmentVariable("SystemDrive") + @"\Program Files\Java\";
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            progressBar1.Visible = true;
            linkLabel1.Text = "下载中";
            //Completed(null, null);
            /*
            WebClient webClient = new WebClient();
            webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);

            string dlurl;
            if (System.IO.Directory.Exists(System.Environment.GetEnvironmentVariable("SystemDrive") + @"\Program Files (x86)") == true)
            {
                dlurl = "http://javadl.sun.com/webapps/download/AutoDL?BundleId=68736";
            }
            else
            {
                dlurl = "http://javadl.sun.com/webapps/download/AutoDL?BundleId=68735";
            }

            webClient.DownloadFileAsync(new Uri(dlurl), Application.StartupPath + @"\java.exe");
            */

            //开始下载
            //Downloader downloader = new Downloader("http://yydl.duowan.com/4/setup/YYSetup-4.17.0.6-zh-CN.exe", Application.StartupPath + @"\java.exe");
            //downloader.Start();
            DownloaderMT downloader = new DownloaderMT("http://yydl.duowan.com/4/setup/YYSetup-4.17.0.6-zh-CN.exe", "d:\\", "yy.exe",5);
            downloader.Start();

            new Thread(() =>
            {
                while (downloader.IsCompleted())
                {
                    progressBar1.Value = (int)(downloader.GetProgress() * 100);
                }
            }).Start();

            //progressBar1.Value = (int)(downloader.GetProgress() * 100);

            /*
            BackgroundWorker bw = new BackgroundWorker();
            bw.DoWork += new DoWorkEventHandler((Object source, DoWorkEventArgs dwe) => { 
                while (!downloader.IsComplete())
                {
                    progressBar1.Value = (int)(downloader.GetProgress() * (float)100);
                    Console.WriteLine((int)(downloader.GetProgress() * (float)100));
                }
               
            });
            bw.RunWorkerAsync();
            */

        }

        

        private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            //MessageBox.Show(azurl);
            //JRESilentInstall js = new JRESilentInstall(Application.StartupPath + @"\java.exe",azurl,true,false);
            linkLabel1.Text = "安装中";
            progressBar1.Visible = false;
            //js.Start();
        }

        #endregion

        #region 按钮事件
        //返回&主页 按钮
        private void pictureBox3_Click(object sender, EventArgs e) 
        {
            //ChangePanel(0);
            Panel panel = (Panel)this.GetChildAtPoint(new Point(1, 95));

            if (panel != MainPanel)
            {
                ChangePanel(MainPanel);
            }
        }

        //单击LOGO事件
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }


        private void btnBuild_Click(object sender, EventArgs e)
        {
            ChangePanel(BuildPanel);
        }

        private void btnLaunch_Click(object sender, EventArgs e)
        {
            IniFile ini = new IniFile(Application.StartupPath + @"\config");

            if (ini.ReadInivalue("Main", "lctype") == "0")
            {
                //FIXME: BUG，无法启动
                string cdo = @"-cp """ +
                             ini.ReadInivalue("Main", "mcpath") +
                             @"\bin\*" +
                             @""" -Djava.library.path=""" +
                             ini.ReadInivalue("Main", "mcpath") +
                             @"\bin\natives""";
                string ccom = ini.ReadInivalue("Main", "lccommand").Replace("{DO}", cdo);
                Clipboard.SetDataObject(ccom);

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = "java";
                psi.Arguments = ccom;

                Process.Start(psi);
            }
            else
            {
                try
                {
                    System.Diagnostics.Process.Start(ini.ReadInivalue("Main", "lcpath"));
                }
                catch {
                    MessageBox.Show("您所指定的启动器不存在！");
                }
            }
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
            Setting frmset = new Setting();
            frmset.ShowDialog();
        }

        private void btnHehe_Click(object sender, EventArgs e)
        {
            //ChangePanel(2);
            ChangePanel(hehePanel);
        }

        #endregion

        #region 呵呵体检功能

        /// <summary>
        /// 体检按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            IniFile ini = new IniFile(Application.StartupPath + @"\config");
            string mcpath = ini.ReadInivalue("Main", "mcpath");

            HeheExamination.SetConfig(mcpath, richTextBox1, label6, linkLabel2);
            int i = HeheExamination.GetState();
            if (i == 0)
            {
                HeheExamination.StartChecking();
            }
            else if (i != 0)
            {
                HeheExamination.OptiMinecraft();
            }
        }

        #endregion

        /// <summary>
        /// 更多按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            heheMenu.Show(Control.MousePosition);
        }

        private void 备份存档ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ResExplorer re = new ResExplorer();
            re.Show();
        }

        private void btnSwitchPage_Click(object sender, EventArgs e)
        {
            //NOTICE:测试中动画效果
            PanelSwitchFX psfx = new PanelSwitchFX(this,MainPanel,SecondPanel,PanelSwitchFX.SWITCH_TO_LEFT,300);
            psfx.Show();
        }

        private void pictureBox12_Click(object sender, EventArgs e)
        {
            PanelSwitchFX psfx = new PanelSwitchFX(this, SecondPanel, MainPanel, PanelSwitchFX.SWITCH_TO_RIGHT, 300);
            psfx.Show();
        }

    }
}